package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * <p></p>
 *
 * @author bin.zhang
 * <p/>
 * Revision History:
 * 2020/09/09, 初始化版本
 * @version 1.0
 **/
public class LengthOfLongestSubstring {

	private static int lengthOfLongestSubstring(String s){
		Map<Character, Integer> map = new HashMap<>();
		int length = 0;
		int curso = 0;
		int strLength = s.length();
		for (int i = 0; i < strLength; i++){
			char seg =s.charAt(i);
			if(map.containsKey(seg)){
				int orgPosition = map.get(seg);
				length = Math.max(i - curso, length);
				curso = Math.max(orgPosition + 1, curso);
			}
			map.put(seg, i);
		}
		return Math.max(strLength - curso, length);
	}

	public static void main(String[] args){
//		System.out.println(lengthOfLongestSubstring("dvdf"));
//		System.out.println(lengthOfLongestSubstring("abcabcbb"));
//		System.out.println(lengthOfLongestSubstring("bbbbb"));
//		System.out.println(lengthOfLongestSubstring(""));
//		System.out.println(lengthOfLongestSubstring(" "));
		System.out.println(lengthOfLongestSubstring("abba"));
	}


}
